package com.example.newproject_overseas.Screen


import android.content.Context
import android.util.Log
import android.view.inputmethod.InputMethodManager
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material.rememberModalBottomSheetState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.example.newproject_overseas.R
import com.example.newproject_overseas.ViewModel.JobViewModel
import com.example.newproject_overseas.components.TopApp
import com.example.newproject_overseas.components.TwoLevelAddress
import com.example.newproject_overseas.components.TwoLevelDropdown
import com.example.newproject_overseas.model.entity.Province
import kotlinx.coroutines.launch
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.GlobalScope

@OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterialApi::class)
@Composable
fun WorkInfomationScreen(navController: NavHostController,
                         jobViewModel: JobViewModel = viewModel()) {
    var monthincome by remember { mutableStateOf("") } //textare变量
    var companyname by remember { mutableStateOf("") }
    var companyaddress by remember { mutableStateOf("") }
    var companypincode by remember { mutableStateOf("") }
    val focusRequester  = remember { FocusRequester() }
    val focusRequester1 = remember { FocusRequester() }
    val focusRequester2 = remember { FocusRequester() }
    val focusRequester3 = remember { FocusRequester() }
    //androidApi
    val contexts = LocalContext.current ;
    var LocalView = LocalView.current ;

    //点击其他屏幕隐藏键盘
    val focusManager = LocalFocusManager.current

    //2级联动（获取工作岗位）
    val bottomSheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()
    var OccupationVal by remember { mutableStateOf("") }

    //2级联动（获取地址信息）
    val AddressSheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
    val scope1 = rememberCoroutineScope()
    var AddressVal by remember { mutableStateOf("") }

    //网络请求
    // 定义Compose中的State来存储工作岗位信息
    LaunchedEffect(Unit){
        //获取工作岗位信息
        jobViewModel.JobStationReq(navController);
        //获取地址信息
        jobViewModel.addressReq(navController)
        Log.i("effect", "effect: ")
        var first = jobViewModel.addressModel?.get(0)


        //异步携程加载
//        GlobalScope.launch {
//            Log.i("GlobalScope", "GlobalScope: ")
//            first?.let {
//                jobViewModel.addressReq2(navController, first.id, first);
//            }
//        }
    }




    Box(modifier = Modifier
        .fillMaxSize()
        .background(color = Color(0XFFEFF0F3))
        .clickable(onClick = {
            Log.e("点击了屏幕", "AskQuestionsChat: ",)
            focusManager.clearFocus() //清除光标

            // 此处使用 Android 平台的 API 隐藏输入法
            val inputMethodManager = contexts.getSystemService(
                Context.INPUT_METHOD_SERVICE
            ) as? InputMethodManager
            inputMethodManager?.hideSoftInputFromWindow(
                LocalView.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        })){

        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(color = Color(0XFFEFF0F3)),

            ) {

            TopApp(title = "Work information", navController = navController)

            Column(
                modifier = Modifier
                    .padding(top = 20.dp)
                    .padding(horizontal = 12.dp)
            ) {
                Column(
                    modifier = Modifier
                        .clickable {
                            scope.launch { bottomSheetState.show() }
                        }
                        .shadow(7.5.dp, shape = RoundedCornerShape(7.5.dp), clip = true)
                        .fillMaxWidth()
                        .height(50.dp)
                        .clip(RoundedCornerShape(7.5.dp)) // 设置圆角为36dp
                        .background(Color.White),


                ) {
                    Row(modifier = Modifier
                        .padding(top = 15.dp)
                        .padding(horizontal = 20.dp)
                    ) {
                        Text(text = "Occupation")
                        Spacer(modifier = Modifier.weight(1f))
                        Image(
                            painter = painterResource(id = R.drawable.go_in),
                            contentDescription = null,
                            modifier = Modifier
                                .width(20.dp)
                                .height(20.dp)
                                .scale(0.9f)
                        )
                    }


                }
                if(OccupationVal.isNotBlank()){
                    Column(modifier = Modifier
                        .padding(bottom = 10.dp)
                        .fillMaxWidth()
                        .background(Color(0xffffffff))
                        .padding(vertical = 15.dp, horizontal = 15.dp)
                        .clip(RoundedCornerShape(7.5.dp)) // 设置圆角为36dp
                    ) {
                        Text(text = OccupationVal, modifier = Modifier,
                            color = Color.Gray,
                            fontSize = 12.sp
                        )
                    }
                }


                Box(
                    modifier = Modifier
                        .padding(top = 20.dp)
                        .shadow(7.5.dp, shape = RoundedCornerShape(7.5.dp), clip = true)
                        .fillMaxWidth()
//                        .height(100.dp)
                        .clip(RoundedCornerShape(7.5.dp)) // 设置圆角为36dp
                        .background(Color.White)
                ){
                    Column(modifier = Modifier
                        .padding(10.dp)
                        .fillMaxWidth()
                        .height(90.dp)
                        .background(Color(0xFFF1F1F1))
                        .padding(vertical = 15.dp, horizontal = 15.dp)
                    ) {
                        Row(modifier = Modifier.height(25.dp)) {
                           Column(modifier = Modifier.weight(1f)) {
                               Text(text = "Month Income(₹)", modifier = Modifier)
                           }

                            //输入框
                            Box(
                                modifier = Modifier.weight(1f)
                            ) {
                                BasicTextField(modifier = Modifier
                                    .fillMaxWidth()
                                    .background(Color(0xFFF1F1F1))
                                    .focusRequester(focusRequester)
                                    .onFocusChanged { focusState ->
                                        if (focusState.isFocused) {
                                            focusRequester.requestFocus()
                                        }
                                    },
                                    value = monthincome, onValueChange = { monthincome = it },
                                    keyboardOptions = KeyboardOptions.Default.copy(
                                        imeAction = ImeAction.Done,
                                        keyboardType = KeyboardType.Text
                                    ),
                                    textStyle = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.Black
                                    ),
                                    singleLine = false
                                )
                                if (monthincome.isEmpty()) {
                                    Spacer(modifier = Modifier);
                                    Box(modifier = Modifier
                                        .fillMaxSize(),
                                        contentAlignment = Alignment.Center
                                        ) {

                                        androidx.compose.material.Text(
                                            text = "Please enter Month Income(₹)",
                                            style = TextStyle(fontSize = 10.sp, color = Color.Gray),
                                            modifier = Modifier,
                                        )
                                    }


                                }

                            }
                        }

                        Spacer(modifier = Modifier.height(10.dp))

                        Row() {
                            Column(modifier = Modifier.weight(1f)) {
                                Text(text = "Company Name")
                            }
//                            Text(text = "Please enter Company Name")

                            //输入框
                            Box(
                                modifier = Modifier.weight(1f)
                            ) {
                                BasicTextField(modifier = Modifier
                                    .fillMaxWidth()
                                    .background(Color(0xFFF1F1F1))
                                    .focusRequester(focusRequester1)
                                    .onFocusChanged { focusState ->
                                        if (focusState.isFocused) {
                                            focusRequester1.requestFocus()
                                        }
                                    },
                                    value = companyname, onValueChange = { companyname = it },
                                    keyboardOptions = KeyboardOptions.Default.copy(
                                        imeAction = ImeAction.Done,
                                        keyboardType = KeyboardType.Text
                                    ),
                                    textStyle = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.Black
                                    ),
                                    singleLine = false
                                )
                                if (companyname.isEmpty()) {
                                    Spacer(modifier = Modifier);
                                    Box(modifier = Modifier
                                        .fillMaxSize(),
                                        contentAlignment = Alignment.Center
                                    ) {

                                        androidx.compose.material.Text(
                                            text = "Please enter Month Income(₹)",
                                            style = TextStyle(fontSize = 10.sp, color = Color.Gray),
                                            modifier = Modifier,
                                        )
                                    }


                                }

                            }




                        }

                    }

                }
            }

            Column(
                modifier = Modifier
                    .padding(top = 20.dp)
                    .padding(horizontal = 12.dp)
            ) {
                Box(
                    modifier = Modifier
                        .shadow(7.5.dp, shape = RoundedCornerShape(7.5.dp), clip = true)
                        .fillMaxWidth()
                        .height(50.dp)
                        .clip(RoundedCornerShape(7.5.dp)) // 设置圆角为36dp
                        .background(Color.White)
                    // .shadow(5.dp, RoundedCornerShape(0.dp, 0.dp, 0.dp, 0.dp))
                ) {
                    Row(modifier = Modifier
                        .padding(top = 15.dp)
                        .padding(horizontal = 20.dp)
                        .clickable {
                            Log.i("点击了", "WorkInfomationScreen: ")
                            navController.navigate("OneLevel")
                        }
                    ) {
                        Text(text = "Company Address")
                        Spacer(modifier = Modifier.weight(1f))
                        Image(
                            painter = painterResource(id = R.drawable.go_in),
                            contentDescription = null,
                            modifier = Modifier
                                .width(20.dp)
                                .height(20.dp)
                                .scale(0.9f)
                        )
                    }
                }

                Box(
                    modifier = Modifier
                        .padding(top = 20.dp)
                        .shadow(7.5.dp, shape = RoundedCornerShape(7.5.dp), clip = true)
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(7.5.dp)) // 设置圆角为36dp
                        .background(Color.White)
                ){
                    Column(modifier = Modifier
                        .padding(10.dp)
                        .fillMaxWidth()
                        .height(90.dp)
                        .background(Color(0xFFF1F1F1))
                        .padding(vertical = 15.dp, horizontal = 15.dp)
                    ) {
                        Row(modifier = Modifier.height(25.dp)) {
                            Column(modifier = Modifier.weight(1f)) {
                                Text(text = "Company Address Details")
                            }
                            //输入框
                            Box(
                                modifier = Modifier.weight(1f)
                            ) {
                                BasicTextField(modifier = Modifier
                                    .fillMaxWidth()
                                    .background(Color(0xFFF1F1F1))
                                    .focusRequester(focusRequester2)
                                    .onFocusChanged { focusState ->
                                        if (focusState.isFocused) {
                                            focusRequester2.requestFocus()
                                        }
                                    },
                                    value = companyaddress, onValueChange = { companyaddress = it },
                                    keyboardOptions = KeyboardOptions.Default.copy(
                                        imeAction = ImeAction.Done,
                                        keyboardType = KeyboardType.Text
                                    ),
                                    textStyle = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.Black
                                    ),
                                    singleLine = false
                                )
                                if (companyaddress.isEmpty()) {
                                    Spacer(modifier = Modifier);
                                    Box(modifier = Modifier
                                        .fillMaxSize(),
                                        contentAlignment = Alignment.Center
                                    ) {

                                        androidx.compose.material.Text(
                                            text = "Please enter Company...",
                                            style = TextStyle(fontSize = 10.sp, color = Color.Gray),
                                            modifier = Modifier,
                                        )
                                    }


                                }

                            }


                        }



                        Row() {
                            Column(modifier = Modifier.weight(1f)) {
                                Text(text = "Company Pincode")
                            }

                            //输入框
                            Box(
                                modifier = Modifier.weight(1f)
                            ) {
                                BasicTextField(modifier = Modifier
                                    .fillMaxWidth()
                                    .background(Color(0xFFF1F1F1))
                                    .focusRequester(focusRequester3)
                                    .onFocusChanged { focusState ->
                                        if (focusState.isFocused) {
                                            focusRequester3.requestFocus()
                                        }
                                    },
                                    value = companypincode, onValueChange = { companypincode = it },
                                    keyboardOptions = KeyboardOptions.Default.copy(
                                        imeAction = ImeAction.Done,
                                        keyboardType = KeyboardType.Text
                                    ),
                                    textStyle = TextStyle(
                                        fontSize = 16.sp,
                                        color = Color.Black
                                    ),
                                    singleLine = false
                                )
                                if (companypincode.isEmpty()) {
                                    Spacer(modifier = Modifier);
                                    Box(modifier = Modifier
                                        .fillMaxSize(),
                                        contentAlignment = Alignment.Center
                                    ) {

                                        androidx.compose.material.Text(
                                            text = "Please enter Company Pincode",
                                            style = TextStyle(fontSize = 10.sp, color = Color.Gray),
                                            modifier = Modifier,
                                        )
                                    }


                                }

                            }


                        }

                    }

                }
            }


        }

        //底部按钮
        Button(onClick = { /*TODO*/ }, modifier = Modifier
            .fillMaxWidth(1f)
            .height(44.dp)
            .align(Alignment.BottomCenter),
            colors = ButtonDefaults.buttonColors(Color(0xFFFFCC00)),
            shape = RoundedCornerShape(4.5.dp)) {
            androidx.compose.material.Text(
                modifier = Modifier,
                text = "Submit",
                color = Color(0xFFFFFFFF),
                fontSize = 20.sp
            )
        }
    }

    //二级联动（获取工作岗位信息）
    jobViewModel.model?.let {
        TwoLevelDropdown(state = bottomSheetState,provinces = it,
            onItemSelected = { data ->
                Log.e( "TwoLevelDropdown: ", data.toString())
                OccupationVal = data.name
        })
    }

//    //二级联动（获取地址信息）
//    jobViewModel.addressModel?.let {
//        TwoLevelAddress(state = AddressSheetState,provinces = it,
//            onItemSelected = { data ->
//                Log.e( "address: ", data.toString())
//                AddressVal = data.name
//            },
//            onItemSelectId = { data ->
//                Log.i("获取id: ", data.toString())
//                var id = data.id;
//                GlobalScope.launch {
//                    Log.i("GlobalScope", "GlobalScope: ")
//                    jobViewModel.addressReq2(navController, id, data);
//                }
//            })
//    }
}



@Preview
@Composable
fun WorkInfomationScreenPreview() {
    val navController = rememberNavController() // 假设这里使用了导航控制器
    WorkInfomationScreen(navController)

}

